﻿using KaroglanSharedLib;
using System.Diagnostics;
using System.IO;

namespace KaroglanLauncher
{
    public class Launcher
    {

        public static string cmdLauncher = @"bin\java.exe";
        public static string USERNAME = "Tollainmear";
        public static int RAMMAX = 4096;
        public static bool JoinServerAfterStart = true;
        public static string ServerUrl = "mc.rover.studio:57591";
        public static string args =
            @"-XX:+UseG1GC " +
            @"-XX:-UseAdaptiveSizePolicy " +
            @"-XX:+ExplicitGCInvokesConcurrent " +
            @"-XX:-OmitStackTraceInFastThrow " +
            @"-Dfml.ignoreInvalidMinecraftCertificates=True " +
            @"-Dfml.ignorePatchDiscrepancies=True " +
            @"-Dlog4j2.formatMsgNoLookups=true " +
            @"-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump " +
            @"-Djava.library.path="".\versions\Karoglan-TFC-Create\Karoglan-TFC-Create-natives"" " +
            @"-Djna.tmpdir="".\versions\Karoglan-TFC-Create\Karoglan-TFC-Create-natives"" " +
            @"-Dorg.lwjgl.system.SharedLibraryExtractPath="".\versions\Karoglan-TFC-Create\Karoglan-TFC-Create-natives"" " +
            @"-Dio.netty.native.workdir="".\versions\Karoglan-TFC-Create\Karoglan-TFC-Create-natives"" " +
            @"-Dminecraft.launcher.brand=KaroglanTech " +
            @"-Dminecraft.launcher.version=313 " +
            @"-cp "".\libraries\com\github\oshi\oshi-core\6.2.2\oshi-core-6.2.2.jar;.\libraries\com\google\code\gson\gson\2.10\gson-2.10.jar;.\libraries\com\google\guava\failureaccess\1.0.1\failureaccess-1.0.1.jar;.\libraries\com\google\guava\guava\31.1-jre\guava-31.1-jre.jar;.\libraries\com\ibm\icu\icu4j\71.1\icu4j-71.1.jar;.\libraries\com\mojang\authlib\4.0.43\authlib-4.0.43.jar;.\libraries\com\mojang\blocklist\1.0.10\blocklist-1.0.10.jar;.\libraries\com\mojang\brigadier\1.1.8\brigadier-1.1.8.jar;.\libraries\com\mojang\datafixerupper\6.0.8\datafixerupper-6.0.8.jar;.\libraries\com\mojang\logging\1.1.1\logging-1.1.1.jar;.\libraries\com\mojang\patchy\2.2.10\patchy-2.2.10.jar;.\libraries\com\mojang\text2speech\1.17.9\text2speech-1.17.9.jar;.\libraries\commons-codec\commons-codec\1.15\commons-codec-1.15.jar;.\libraries\commons-io\commons-io\2.11.0\commons-io-2.11.0.jar;.\libraries\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;.\libraries\io\netty\netty-buffer\4.1.82.Final\netty-buffer-4.1.82.Final.jar;.\libraries\io\netty\netty-codec\4.1.82.Final\netty-codec-4.1.82.Final.jar;.\libraries\io\netty\netty-common\4.1.82.Final\netty-common-4.1.82.Final.jar;.\libraries\io\netty\netty-handler\4.1.82.Final\netty-handler-4.1.82.Final.jar;.\libraries\io\netty\netty-resolver\4.1.82.Final\netty-resolver-4.1.82.Final.jar;.\libraries\io\netty\netty-transport-classes-epoll\4.1.82.Final\netty-transport-classes-epoll-4.1.82.Final.jar;.\libraries\io\netty\netty-transport-native-unix-common\4.1.82.Final\netty-transport-native-unix-common-4.1.82.Final.jar;.\libraries\io\netty\netty-transport\4.1.82.Final\netty-transport-4.1.82.Final.jar;.\libraries\it\unimi\dsi\fastutil\8.5.9\fastutil-8.5.9.jar;.\libraries\net\java\dev\jna\jna-platform\5.12.1\jna-platform-5.12.1.jar;.\libraries\net\java\dev\jna\jna\5.12.1\jna-5.12.1.jar;.\libraries\net\sf\jopt-simple\jopt-simple\5.0.4\jopt-simple-5.0.4.jar;.\libraries\org\apache\commons\commons-compress\1.21\commons-compress-1.21.jar;.\libraries\org\apache\commons\commons-lang3\3.12.0\commons-lang3-3.12.0.jar;.\libraries\org\apache\httpcomponents\httpclient\4.5.13\httpclient-4.5.13.jar;.\libraries\org\apache\httpcomponents\httpcore\4.4.15\httpcore-4.4.15.jar;.\libraries\org\apache\logging\log4j\log4j-api\2.19.0\log4j-api-2.19.0.jar;.\libraries\org\apache\logging\log4j\log4j-core\2.19.0\log4j-core-2.19.0.jar;.\libraries\org\apache\logging\log4j\log4j-slf4j2-impl\2.19.0\log4j-slf4j2-impl-2.19.0.jar;.\libraries\org\joml\joml\1.10.5\joml-1.10.5.jar;.\libraries\org\lwjgl\lwjgl-glfw\3.3.1\lwjgl-glfw-3.3.1.jar;.\libraries\org\lwjgl\lwjgl-glfw\3.3.1\lwjgl-glfw-3.3.1-natives-windows.jar;.\libraries\org\lwjgl\lwjgl-glfw\3.3.1\lwjgl-glfw-3.3.1-natives-windows-arm64.jar;.\libraries\org\lwjgl\lwjgl-glfw\3.3.1\lwjgl-glfw-3.3.1-natives-windows-x86.jar;.\libraries\org\lwjgl\lwjgl-jemalloc\3.3.1\lwjgl-jemalloc-3.3.1.jar;.\libraries\org\lwjgl\lwjgl-jemalloc\3.3.1\lwjgl-jemalloc-3.3.1-natives-windows.jar;.\libraries\org\lwjgl\lwjgl-jemalloc\3.3.1\lwjgl-jemalloc-3.3.1-natives-windows-arm64.jar;.\libraries\org\lwjgl\lwjgl-jemalloc\3.3.1\lwjgl-jemalloc-3.3.1-natives-windows-x86.jar;.\libraries\org\lwjgl\lwjgl-openal\3.3.1\lwjgl-openal-3.3.1.jar;.\libraries\org\lwjgl\lwjgl-openal\3.3.1\lwjgl-openal-3.3.1-natives-windows.jar;.\libraries\org\lwjgl\lwjgl-openal\3.3.1\lwjgl-openal-3.3.1-natives-windows-arm64.jar;.\libraries\org\lwjgl\lwjgl-openal\3.3.1\lwjgl-openal-3.3.1-natives-windows-x86.jar;.\libraries\org\lwjgl\lwjgl-opengl\3.3.1\lwjgl-opengl-3.3.1.jar;.\libraries\org\lwjgl\lwjgl-opengl\3.3.1\lwjgl-opengl-3.3.1-natives-windows.jar;.\libraries\org\lwjgl\lwjgl-opengl\3.3.1\lwjgl-opengl-3.3.1-natives-windows-arm64.jar;.\libraries\org\lwjgl\lwjgl-opengl\3.3.1\lwjgl-opengl-3.3.1-natives-windows-x86.jar;.\libraries\org\lwjgl\lwjgl-stb\3.3.1\lwjgl-stb-3.3.1.jar;.\libraries\org\lwjgl\lwjgl-stb\3.3.1\lwjgl-stb-3.3.1-natives-windows.jar;.\libraries\org\lwjgl\lwjgl-stb\3.3.1\lwjgl-stb-3.3.1-natives-windows-arm64.jar;.\libraries\org\lwjgl\lwjgl-stb\3.3.1\lwjgl-stb-3.3.1-natives-windows-x86.jar;.\libraries\org\lwjgl\lwjgl-tinyfd\3.3.1\lwjgl-tinyfd-3.3.1.jar;.\libraries\org\lwjgl\lwjgl-tinyfd\3.3.1\lwjgl-tinyfd-3.3.1-natives-windows.jar;.\libraries\org\lwjgl\lwjgl-tinyfd\3.3.1\lwjgl-tinyfd-3.3.1-natives-windows-arm64.jar;.\libraries\org\lwjgl\lwjgl-tinyfd\3.3.1\lwjgl-tinyfd-3.3.1-natives-windows-x86.jar;.\libraries\org\lwjgl\lwjgl\3.3.1\lwjgl-3.3.1.jar;.\libraries\org\lwjgl\lwjgl\3.3.1\lwjgl-3.3.1-natives-windows.jar;.\libraries\org\lwjgl\lwjgl\3.3.1\lwjgl-3.3.1-natives-windows-arm64.jar;.\libraries\org\lwjgl\lwjgl\3.3.1\lwjgl-3.3.1-natives-windows-x86.jar;.\libraries\org\slf4j\slf4j-api\2.0.1\slf4j-api-2.0.1.jar;.\libraries\cpw\mods\securejarhandler\2.1.10\securejarhandler-2.1.10.jar;.\libraries\org\ow2\asm\asm\9.5\asm-9.5.jar;.\libraries\org\ow2\asm\asm-commons\9.5\asm-commons-9.5.jar;.\libraries\org\ow2\asm\asm-tree\9.5\asm-tree-9.5.jar;.\libraries\org\ow2\asm\asm-util\9.5\asm-util-9.5.jar;.\libraries\org\ow2\asm\asm-analysis\9.5\asm-analysis-9.5.jar;.\libraries\net\minecraftforge\accesstransformers\8.0.4\accesstransformers-8.0.4.jar;.\libraries\org\antlr\antlr4-runtime\4.9.1\antlr4-runtime-4.9.1.jar;.\libraries\net\minecraftforge\eventbus\6.0.5\eventbus-6.0.5.jar;.\libraries\net\minecraftforge\forgespi\7.0.1\forgespi-7.0.1.jar;.\libraries\net\minecraftforge\coremods\5.0.1\coremods-5.0.1.jar;.\libraries\cpw\mods\modlauncher\10.0.9\modlauncher-10.0.9.jar;.\libraries\net\minecraftforge\unsafe\0.2.0\unsafe-0.2.0.jar;.\libraries\net\minecraftforge\mergetool\1.1.5\mergetool-1.1.5-api.jar;.\libraries\com\electronwill\night-config\core\3.6.4\core-3.6.4.jar;.\libraries\com\electronwill\night-config\toml\3.6.4\toml-3.6.4.jar;.\libraries\org\apache\maven\maven-artifact\3.8.5\maven-artifact-3.8.5.jar;.\libraries\net\jodah\typetools\0.6.3\typetools-0.6.3.jar;.\libraries\net\minecrell\terminalconsoleappender\1.2.0\terminalconsoleappender-1.2.0.jar;.\libraries\org\jline\jline-reader\3.12.1\jline-reader-3.12.1.jar;.\libraries\org\jline\jline-terminal\3.12.1\jline-terminal-3.12.1.jar;.\libraries\org\spongepowered\mixin\0.8.5\mixin-0.8.5.jar;.\libraries\org\openjdk\nashorn\nashorn-core\15.3\nashorn-core-15.3.jar;.\libraries\net\minecraftforge\JarJarSelector\0.3.19\JarJarSelector-0.3.19.jar;.\libraries\net\minecraftforge\JarJarMetadata\0.3.19\JarJarMetadata-0.3.19.jar;.\libraries\cpw\mods\bootstraplauncher\1.1.2\bootstraplauncher-1.1.2.jar;.\libraries\net\minecraftforge\JarJarFileSystems\0.3.19\JarJarFileSystems-0.3.19.jar;.\libraries\net\minecraftforge\fmlloader\1.20.1-47.2.20\fmlloader-1.20.1-47.2.20.jar;.\libraries\net\minecraftforge\fmlearlydisplay\1.20.1-47.2.20\fmlearlydisplay-1.20.1-47.2.20.jar;.\versions\Karoglan-TFC-Create\Karoglan-TFC-Create.jar"" " +
            @"-Djava.net.preferIPv6Addresses=system " +
            @"-DignoreList=bootstraplauncher,securejarhandler,asm-commons,asm-util,asm-analysis,asm-tree,asm,JarJarFileSystems,client-extra,fmlcore,javafmllanguage,lowcodelanguage,mclanguage,forge-,Karoglan-TFC-Create.jar " +
            @"-DmergeModules=jna-5.10.0.jar,jna-platform-5.10.0.jar " +
            @"-DlibraryDirectory="".\libraries"" " +
            @"-p "".\libraries""/cpw/mods/bootstraplauncher/1.1.2/bootstraplauncher-1.1.2.jar;"".\libraries""/cpw/mods/securejarhandler/2.1.10/securejarhandler-2.1.10.jar;"".\libraries""/org/ow2/asm/asm-commons/9.5/asm-commons-9.5.jar;"".\libraries""/org/ow2/asm/asm-util/9.5/asm-util-9.5.jar;"".\libraries""/org/ow2/asm/asm-analysis/9.5/asm-analysis-9.5.jar;"".\libraries""/org/ow2/asm/asm-tree/9.5/asm-tree-9.5.jar;"".\libraries""/org/ow2/asm/asm/9.5/asm-9.5.jar;"".\libraries""/net/minecraftforge/JarJarFileSystems/0.3.19/JarJarFileSystems-0.3.19.jar " +
            @"--add-modules ALL-MODULE-PATH " +
            @"--add-opens java.base/java.util.jar=cpw.mods.securejarhandler " +
            @"--add-opens java.base/java.lang.invoke=cpw.mods.securejarhandler " +
            @"--add-exports java.base/sun.security.util=cpw.mods.securejarhandler " +
            @"--add-exports jdk.naming.dns/com.sun.jndi.dns=java.naming " +
            @"-Xmn256m " +
            $"-Xmx{RAMMAX}m " +
            @"-Dlog4j2.formatMsgNoLookups=true " +
            @"--add-exports cpw.mods.bootstraplauncher/cpw.mods.bootstraplauncher=ALL-UNNAMED " +
            @"-Doolloo.jlw.tmpdir=""..\temp"" " +
            @"-jar ""..\tools\JavaWrapper.jar"" cpw.mods.bootstraplauncher.BootstrapLauncher " +
            $"--username {USERNAME} " +
            @"--version Karoglan-TFC-Create " +
            @"--gameDir "".\versions\Karoglan-TFC-Create"" " +
            @"--assetsDir "".\assets"" " +
            @"--assetIndex 5 " +
            @"--uuid 00000XXXXXXXXXXXXXXXXXXXXXX1EA0C " +
            @"--accessToken 00000XXXXXXXXXXXXXXXXXXXXXX1EA0C " +
            @"--clientId ${clientid} " +
            @"--xuid ${auth_xuid} " +
            @"--userType msa " +
            @"--versionType KaroglanTech " +
            @"--width 854 " +
            @"--height 480 " +
            @"--launchTarget forgeclient " +
            @"--fml.forgeVersion 47.2.20 " +
            @"--fml.mcVersion 1.20.1 " +
            @"--fml.forgeGroup net.minecraftforge " +
            @"--fml.mcpVersion 20230612.114412 " +
            (JoinServerAfterStart? $"--quickPlayMultiplayer \"{ServerUrl}\" " : "");

        public static void LaunchGame()
        {
            string wdPath = Path.Combine(Utils.GetBaseFolder(), ".minecraft");
            ProcessStartInfo info = new ProcessStartInfo();
            var javaPath = "";
            if (!Utils.GetJavaPath(out javaPath)) 
            {
                Utils.Error("无法找到有效的Java路径，启动失败。");
            }
            info.FileName = Path.Combine(javaPath, cmdLauncher);
            info.Arguments = args;

            info.UseShellExecute = true;
            info.CreateNoWindow = false;
            info.WorkingDirectory = wdPath;
            var process = Process.Start(info);
            //process
        }
    }
}